package GUI;

import Classes.GoogleMap;
import Classes.Persona;
import Persistencia.CiudadesMapper;
import PureFactoryClasses.ApController;
import java.util.Calendar;
import javax.swing.JOptionPane;

/*
 * newContactoPersonaForm.java
 *
 * Created on 31/08/2011, 10:41:34 PM
 */
/**
 *
 * @author Juampi
 */
public class newContactoPersonaForm extends javax.swing.JDialog {

    Persona persona;
    private boolean contactExist;
    private AgendaView agendaView;
    private boolean isEditing = false;

    /** Creates new form newContactoPersonaForm */
    public newContactoPersonaForm(AgendaView agendaView, boolean modal) {
        super(agendaView.getFrame(), modal);
        this.agendaView = agendaView;
        initComponents();
        contactExist = false;
        this.btnEdit.setVisible(false);
        this.btnAccept.setText("Agregar");
        this.persona = new Persona();
    }

    public newContactoPersonaForm(AgendaView agendaView, boolean modal, Persona persona) {
        super(agendaView.getFrame(), modal);
        String strMes;
        String strDia;
        this.agendaView = agendaView;
        initComponents();
        this.setSize(870, 180);
        this.jLabel1.setText("CONTACTO");
        try {
            this.persona = persona;
            this.txtAño.setText(String.valueOf(persona.getFechaNacimiento().get(Calendar.YEAR)));
            int mes = persona.getFechaNacimiento().get(Calendar.MONTH);
            if (mes < 10) {
                strMes = "0" + String.valueOf(mes);
            } else {
                strMes = String.valueOf(mes);
            }


            this.txtMes.setText(strMes);
            int dia = persona.getFechaNacimiento().get(Calendar.DATE);
            strDia = String.valueOf(dia);
            if (dia < 10) {
                strDia = "0" + strDia;
            }
            this.txtDia.setText(strDia);
            this.txtCP.setText(String.valueOf(persona.getCiudad().getCodigoPostal()));
            this.txtCelular.setText(String.valueOf(persona.getCelular()));
            this.txtDir.setText(persona.getDireccion());
            this.txtEmail.setText(persona.getCorreoElectronico());
            this.txtNombre.setText(persona.getNombre());
            this.txtTel.setText(String.valueOf(persona.getTelefono()));
            this.contactExist = true;
            this.lblMapa.setIcon(GoogleMap.getInstance().getImagenMapa(persona.getDireccion() + " " + persona.getCiudad().getLocalidad(), this.lblMapa.getWidth(), this.lblMapa.getHeight()));
            this.lblMapa.setVisible(true);
            this.btnAccept.setText("Aceptar");

            setEditable(false);

            this.jLabel1.setText(persona.getNombre());
        } catch (Exception ex) {
        }
    }

    private void setEditable(boolean editable) {
        this.txtAño.setEnabled(editable);
        this.txtCP.setEnabled(editable);
        this.txtCelular.setEnabled(editable);
        this.txtDia.setEnabled(editable);
        this.txtDir.setEnabled(editable);
        this.txtEmail.setEnabled(editable);
        this.txtMes.setEnabled(editable);
        this.txtNombre.setEnabled(editable);
        this.txtTel.setEnabled(editable);
        btnEd.setVisible(!editable);
        btnDelete.setVisible(contactExist);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnEdit = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtDir = new javax.swing.JTextField();
        txtCP = new javax.swing.JTextField();
        txtTel = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtCelular = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        btnAccept = new javax.swing.JButton();
        txtDia = new javax.swing.JTextField();
        txtMes = new javax.swing.JTextField();
        txtAño = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        lblMapa = new javax.swing.JLabel();
        lblMap = new javax.swing.JLabel();
        btnEd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(GUI.AgendaApp.class).getContext().getResourceMap(newContactoPersonaForm.class);
        btnEdit.setText(resourceMap.getString("btnEdit.text")); // NOI18N
        btnEdit.setName("btnEdit"); // NOI18N
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(570, 330));
        setName("Form"); // NOI18N
        setResizable(false);

        jPanel2.setName("jPanel2"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel2.add(jLabel1);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START);

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setMinimumSize(new java.awt.Dimension(300, 210));
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(475, 210));

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        txtNombre.setText(resourceMap.getString("txtNombre.text")); // NOI18N
        txtNombre.setName("txtNombre"); // NOI18N

        txtDir.setText(resourceMap.getString("txtDir.text")); // NOI18N
        txtDir.setName("txtDir"); // NOI18N

        txtCP.setText(resourceMap.getString("txtCP.text")); // NOI18N
        txtCP.setName("txtCP"); // NOI18N

        txtTel.setText(resourceMap.getString("txtTel.text")); // NOI18N
        txtTel.setName("txtTel"); // NOI18N

        txtEmail.setName("txtEMail"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        txtCelular.setText(resourceMap.getString("txtCelular.text")); // NOI18N
        txtCelular.setName("txtCelular"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnAccept.setText(resourceMap.getString("btnAccept.text")); // NOI18N
        btnAccept.setMaximumSize(new java.awt.Dimension(75, 23));
        btnAccept.setMinimumSize(new java.awt.Dimension(75, 23));
        btnAccept.setName("btnAccept"); // NOI18N
        btnAccept.setPreferredSize(new java.awt.Dimension(75, 23));
        btnAccept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAcceptActionPerformed(evt);
            }
        });

        txtDia.setText(resourceMap.getString("txtDia.text")); // NOI18N
        txtDia.setToolTipText(resourceMap.getString("txtDia.toolTipText")); // NOI18N
        txtDia.setName("txtDia"); // NOI18N

        txtMes.setText(resourceMap.getString("txtMes.text")); // NOI18N
        txtMes.setToolTipText(resourceMap.getString("txtMes.toolTipText")); // NOI18N
        txtMes.setName("txtMes"); // NOI18N

        txtAño.setText(resourceMap.getString("txtAño.text")); // NOI18N
        txtAño.setToolTipText(resourceMap.getString("txtAño.toolTipText")); // NOI18N
        txtAño.setName("txtAño"); // NOI18N

        jPanel4.setName("jPanel4"); // NOI18N

        lblMapa.setText(resourceMap.getString("lblMapa.text")); // NOI18N
        lblMapa.setName("lblMapa"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(lblMapa, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblMapa, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        lblMap.setText(resourceMap.getString("lblMap.text")); // NOI18N
        lblMap.setName("lblMap"); // NOI18N

        btnEd.setText(resourceMap.getString("btnEd.text")); // NOI18N
        btnEd.setMaximumSize(new java.awt.Dimension(75, 23));
        btnEd.setMinimumSize(new java.awt.Dimension(75, 23));
        btnEd.setName("btnEd"); // NOI18N
        btnEd.setPreferredSize(new java.awt.Dimension(75, 23));
        btnEd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText(resourceMap.getString("btnDelete.text")); // NOI18N
        btnDelete.setMaximumSize(new java.awt.Dimension(75, 23));
        btnDelete.setMinimumSize(new java.awt.Dimension(75, 23));
        btnDelete.setName("btnDelete"); // NOI18N
        btnDelete.setPreferredSize(new java.awt.Dimension(75, 23));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel9))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtEmail)
                                    .addComponent(txtCP)
                                    .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))))
                        .addGap(30, 30, 30)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel8))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtDir, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(txtDia, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtMes, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtAño, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(txtTel, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCelular, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                        .addGap(26, 26, 26))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEd, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAccept, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)))
                .addComponent(lblMap, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAccept, jButton1});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMap, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(txtDia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAño, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtCP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel9)
                                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel8)
                                    .addComponent(txtTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(btnAccept, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnEd, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnAccept, jButton1});

        getContentPane().add(jPanel3, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAcceptActionPerformed
        ApController apController = ApController.getInstance();
        if (validarDatos()) {
            persona.setCelular(Integer.valueOf(this.txtCelular.getText()));
            persona.setNombre(txtNombre.getText());
            persona.setCiudad(CiudadesMapper.getInstance().getCiudad(Integer.valueOf(txtCP.getText())));
            persona.setCorreoElectronico(txtEmail.getText());
            persona.setDireccion(this.txtDir.getText());
            Calendar fecha = Calendar.getInstance();
            fecha.set(Integer.valueOf(txtAño.getText()), Integer.valueOf(txtMes.getText()), Integer.valueOf(txtDia.getText()));
            persona.setFechaNacimiento(fecha);
            persona.setTelefono(Integer.valueOf(txtTel.getText()));

            if (!this.contactExist) {
                if (!apController.agregarPersona(persona)) {
                    JOptionPane.showMessageDialog(null, "Error de conexion a la base de datos");
                }
                agendaView.loadContactTable();
            } else {
                if (isEditing) {
                    if (!apController.modificarPersona(persona)) {
                        JOptionPane.showMessageDialog(null, "Error de conexion con la Base de datos");
                    }
                    agendaView.loadContactTable();
                }
            }
            this.dispose();
        }
    }//GEN-LAST:event_btnAcceptActionPerformed
    private boolean validarDatos() {
        if (!this.txtCP.getText().matches("[0-9]+")) {
            JOptionPane.showMessageDialog(null, "El codigo postal es incorrecto.");
            return false;
        }
        if (!this.txtCelular.getText().matches(("[0-9]+"))) {
            JOptionPane.showMessageDialog(null, "El celular no es valido.");
            return false;
        }
        if ((this.txtDia.getText() == null) || (this.txtDia.getText().equals(""))) {
            JOptionPane.showMessageDialog(null, "El dia es incorrecto");
            return false;
        }
        if ((this.txtDir.getText() == null) || (this.txtDir.getText().equals(""))) {
            JOptionPane.showMessageDialog(null, "La direccion esta incompleta.");
            return false;
        }
        if ((!this.txtEmail.getText().matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"))) {
            JOptionPane.showMessageDialog(null, "El email no es valido.");
            return false;
        }
        if ((!this.txtMes.getText().matches("\\d{2}"))) {
            JOptionPane.showMessageDialog(null, "El mes no es valido");
            return false;
        }
        if ((!this.txtAño.getText().matches("\\d{4}"))) {
            JOptionPane.showMessageDialog(null, "El año no es valido");
            return false;
        }
        if ((this.txtNombre.getText() == null) || (this.txtNombre.getText().equals(""))) {
            JOptionPane.showMessageDialog(null, "El nombre no esta completo.");
            return false;
        }
        if (!this.txtTel.getText().matches("[0-9]{4,10}")) {
            JOptionPane.showMessageDialog(null, "El telefono no es correcto.");
            return false;
        }

        return true;
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        this.setEditable(true);
        this.isEditing = true;
        this.btnAccept.setText("Guardar");
        this.btnDelete.setText("Borrar");
        this.btnDelete.setVisible(true);
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnEdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEdActionPerformed
    }//GEN-LAST:event_btnEdActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        if ((JOptionPane.showConfirmDialog(this, "¿Confirma que desea eliminar el contacto?") == JOptionPane.OK_OPTION)) {
            if (!ApController.getInstance().borrarContacto(persona)) {
                JOptionPane.showMessageDialog(null, "Error de conexion a la base de datos");
            } else {
                agendaView.loadContactTable();
                this.dispose();
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAccept;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEd;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel lblMap;
    private javax.swing.JLabel lblMapa;
    private javax.swing.JTextField txtAño;
    private javax.swing.JTextField txtCP;
    private javax.swing.JTextField txtCelular;
    private javax.swing.JTextField txtDia;
    private javax.swing.JTextField txtDir;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtMes;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtTel;
    // End of variables declaration//GEN-END:variables
}
